<template>
  <el-drawer
    title="切换菜单"
    :visible.sync="drawerVisible"
    direction="rtl"
    size="85%"
    class="menu-drawer"
    @close="handleClose">
    <div class="menu-container">
      <!-- 左侧主菜单 -->
      <div class="main-menu">
        <div 
          class="main-menu-item" 
          :class="{ active: selectedMainMenuId === menu.id }"
          v-for="menu in menuList" 
          :key="menu.id"
          @click="selectMainMenu(menu)">
          <span>{{ menu.name }}</span>
        </div>
      </div>
      
      <!-- 右侧子菜单 -->
      <div class="sub-menu">
        <div 
          class="sub-menu-item"
          :class="{ active: currentSubMenuId === subMenu.id }"
          v-for="subMenu in currentSubMenuList"
          :key="subMenu.id"
          @click="selectSubMenu(subMenu)">
          <span>{{ subMenu.name }}</span>
          <i 
            v-if="currentSubMenuId === subMenu.id" 
            class="el-icon-check check-icon"
          ></i>
        </div>
      </div>
    </div>
  </el-drawer>
</template>

<script>
export default {
  name: 'MenuDrawer',
  
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    menuList: {
      type: Array,
      default: () => []
    },
    currentSubMenuId: {
      type: Number,
      default: null
    }
  },
  
  data() {
    return {
      selectedMainMenuId: null,
      currentSubMenuList: []
    };
  },
  
  computed: {
    drawerVisible: {
      get() {
        return this.visible;
      },
      set(val) {
        this.$emit('update:visible', val);
      }
    }
  },
  
  methods: {
    selectMainMenu(menu) {
      this.selectedMainMenuId = menu.id;
      this.currentSubMenuList = menu.children || [];
    },
    
    selectSubMenu(subMenu) {
      this.$emit('select-menu', subMenu);
    },
    
    handleClose() {
      this.$emit('update:visible', false);
    },
    
    initMenu() {
      // 根据当前选中的子菜单，初始化主菜单选中状态
      if (this.currentSubMenuId) {
        for (const menu of this.menuList) {
          const hasSubMenu = menu.children?.some(sub => sub.id === this.currentSubMenuId);
          if (hasSubMenu) {
            this.selectedMainMenuId = menu.id;
            this.currentSubMenuList = menu.children;
            return;
          }
        }
      }
      
      // 默认选中第一个主菜单
      if (this.menuList.length > 0) {
        const firstMenu = this.menuList[0];
        this.selectedMainMenuId = firstMenu.id;
        this.currentSubMenuList = firstMenu.children || [];
      }
    }
  },
  
  watch: {
    visible(val) {
      if (val) {
        this.initMenu();
      }
    }
  },
  
  mounted() {
    this.initMenu();
  }
};
</script>

<style scoped>
.menu-container {
  display: flex;
  height: 100%;
  background: #f5f5f5;
}

/* 左侧主菜单 */
.main-menu {
  width: 140px;
  background: #f7f8fa;
  overflow-y: auto;
}

.main-menu-item {
  padding: 18px 15px;
  font-size: 14px;
  color: #333;
  cursor: pointer;
  transition: all 0.3s;
  text-align: center;
  position: relative;
}

.main-menu-item:active {
  background: #eff0f2;
}

.main-menu-item.active {
  background: white;
  color: #333;
  font-weight: 400;
}

.main-menu-item.active::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 3px;
  height: 16px;
  background: #ff3b30;
  border-radius: 0 2px 2px 0;
}

/* 右侧子菜单 */
.sub-menu {
  flex: 1;
  background: white;
  overflow-y: auto;
}

.sub-menu-item {
  padding: 15px 20px;
  font-size: 14px;
  color: #666;
  cursor: pointer;
  transition: all 0.3s;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.sub-menu-item:hover {
  background: #eff0f2;
}

.sub-menu-item:active {
  background: #e8e9eb;
}

.sub-menu-item.active {
  background: white;
  color: #ff3b30;
  font-weight: 400;
}

.check-icon {
  font-size: 18px;
  color: #ff3b30;
  font-weight: bold;
  margin-left: 10px;
}

/* 滚动条样式 */
.main-menu::-webkit-scrollbar,
.sub-menu::-webkit-scrollbar {
  width: 4px;
}

.main-menu::-webkit-scrollbar-thumb,
.sub-menu::-webkit-scrollbar-thumb {
  background: #ddd;
  border-radius: 2px;
}
</style>

<style>
.menu-drawer .el-drawer__header {
  margin-bottom: 0;
  padding: 15px 20px;
  border-bottom: 1px solid #e8e8e8;
}

.menu-drawer .el-drawer__body {
  padding: 0;
}
</style>

